<?php
/**
 * 微信自定义菜单
 * User: James
 * Date: 2016/9/14
 * Time: 10:11
 */

namespace Weixin\Controller;
use Common\Controller\WeixinbaseController;

class MenuController extends WeixinbaseController
{
    protected $create_url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN"; //_post

    protected $query_url="https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN"; //_get

    protected $delete_url="https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN"; //_get

    protected $wxMenuModel ;

    function _initialize()
    {
        parent::_initialize();
        $this->wxMenuModel =new \Weixin\Model\WxMenuModel();
    }


    public function index()
    {
        $result =$this->wxMenuModel->order(array("listorder"=>"asc"))->select();
//        debug($result);
        $tree = new \Tree();
        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        foreach ($result as $r) {
            $r['str_manage'] = '<a href="' . U("Menu/add", array("parent" => $r['term_id'])) . '">添加子菜单</a> | <a href="' . U("Menu/edit", array("id" => $r['term_id'])) . '">编辑</a> | <a class="js-ajax-delete" href="' . U("Menu/delete", array("id" => $r['term_id'])) . '">删除</a> ';
            $r['name']=$r['menu_name'];
            $r['type']=$r['event_type'];
            $r['typestring'] = $r['type_string'];
            $r['status'] =$r['status'] ? "显示":"隐藏";
            $array[] = $r;
        }

        $tree->init($array);
        $str = "<tr>
					<td><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input input-order'></td>
					<td>\$id</td>
					<td>\$spacer <a href='#' target='_blank'>\$name</a></td>
	    			<td>\$type</td>
	    			<td>\$typestring</td>
	    			<td>\$status</td>
					<td>\$str_manage</td>
				</tr>";
        $taxonomys = $tree->get_tree(0, $str);
        $this->assign("taxonomys", $taxonomys);
        $this->display();
    }

    public function add()
    {
        $result =$this->wxMenuModel->order(array("listorder"=>"ASC"))->select();
        $tree = new \Tree();
        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        $tree->init($result);
        $str="<option value='\$id' \$selected>\$spacer\$menu_name</option>";
        $nav_trees = $tree->get_tree(0, $str);

        $this->assign("nav_trees", $nav_trees);
        $this->display();
    }

    public function add_post(){
        if (IS_POST) {
            $data=I("post.");
            if ($this->wxMenuModel->create($data)) {
                $result=$this->wxMenuModel->add();
                if ($result!==false) {
                    $this->success("添加成功！", U("Menu/index"));
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error($this->wxMenuModel->getError());
            }
        }
    }

    public function edit(){
        $this->display();
    }

    public function edit_post(){

    }

    public function delete(){

    }




    public function createMenu(){
        $weiMenuUrl ="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";

        $result =$this->wxMenuModel->where(array("parentid"=>0))->order(array("listorder"=>'asc'))->select();
        foreach ($result as $k=>$r){
            $menu[$k]=$r;
            $menu[$k]['child']=$this->wxMenuModel->where(array("parentid"=>$r['id']))->order(array("listorder"=>"asc"))->select();
        }



        $menus["button"]=array();
        foreach ($menu as $k => $r){
            $menus["button"][$k]["type"]=$r['event_type'];
            $menus["button"][$k]["name"]=$r["menu_name"];
            switch ($r["event_type"]){
                case "view":
                    $menus["button"][$k]["url"]=$r["type_string"];
                    break;
                case "view_limited":
                    $menus["button"][$k]["media_id"]=$r["type_string"];
                    break;
                case "media_id":
                    $menus["button"][$k]["media_id"]=$r["type_string"];
                    break;
                default:
                    $menus["button"][$k]["key"]=$r["type_string"];
            }
            if (is_array($r["child"]) && isset($r["child"])){

                foreach ($r["child"] as $key => $m){
                    $menus["button"][$k]["sub_button"][$key]["type"]=$m["event_type"];
                    $menus["button"][$k]["sub_button"][$key]["name"]=$m["menu_name"];
                    switch ($m["event_type"]){
                        case "view":
                            $menus["button"][$k]["sub_button"][$key]["url"]=$r["type_string"];
                            break;
                        case "view_limited":
                            $menus["button"][$k]["sub_button"][$key]["media_id"]=$r["type_string"];
                            break;
                        case "media_id":
                            $menus["button"][$k]["sub_button"][$key]["media_id"]=$r["type_string"];
                            break;
                        default:
                            $menus["button"][$k]["sub_button"][$key]["key"]=$r["type_string"];
                    }
                }
            }

        }

//        print_r($menus);

        $post_data = json_encode($menus,JSON_UNESCAPED_UNICODE);
        $result =post_weixinApi($weiMenuUrl,$post_data,$this->access_token);
        echo $result;

    }

    public function queryMenu()
    {
        
    }
    
    public function deleteMenu(){
        
    }

    public function publishMenu()
    {

    }
    
    
    
}